package com.epam.kulibaba.helpers.testSupport;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.epam.kulibaba.helpers.Class.Product;



public class SortingEngine {	
	static Comparator<Product> nameComparator = new Comparator<Product>() {	    
	    public int compare(Product o1, Product o2) {
	        return o1.getName().compareTo(o2.getName());
	    }
	};

	static Comparator<Product> priceComparator = new Comparator<Product>() {	   
	    public int compare(Product o1, Product o2) {
	    	if (o1.getPrice() < o2.getPrice())
	    		return -1;
	    	if (o1.getPrice() > o2.getPrice())
	    		return 1;
	        return 0;
	    }
	};

	public static void orderBy( String field, List<Product> productList) {
	    Comparator<Product> comparator = null;
	    if(field.equals("name")) {
	        comparator = nameComparator;
	    } else if (field.equals("price")) {
	        comparator = priceComparator;
	    } else {
	        throw new IllegalArgumentException("Comparator not found for " + field);
	    }

	    Collections.sort(productList, comparator);
	}	
	
}
